<?php 
use \PFT\Db;
include('header.php');
include('/var/www/html/new/d/module/common/Db.class.php');
$db_conf = include('/var/www/html/new/d/module/common/db.conf.php');
Db::Conf($db_conf['remote_1']);
Db::connect();

$themes = file_get_contents('/var/www/html/new/d/common/topic.txt');
$themes = explode(':', $themes);

$config = Db::fetch(
    "select * from pft_b2c_store_config where account=:account limit 1",
    array(':account' => $_SESSION['account'])
);

if ($config) {
    $config['img_urls'] = json_decode(
        htmlspecialchars_decode($config['img_urls']), 
        true
    );
    $config['types'] = explode(',', $config['show_type']);
    $config['themes'] = explode('|', $config['themes']);
    foreach ($config['themes'] as $k => $item) {
        $config['themes'][$k] = explode('__', $item);
    }
} else {
    $config = array(
        'img_urls' => array(),
        'themes' => array(array('A')),
        'types' => explode(',', 'A,B,C,T')
    );
}
?>
<!-- 配置文件 -->
<script type="text/javascript" charset="utf-8" src="http://www.12301.cc/ue/ueditor_config_mini.js"></script>
<script type="text/javascript" src="http://www.12301.cc/ue_126/ueditor.all.min_dev.js"></script>

<title>b2c店铺手机端配置</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-static-top" role="navigation" style="position:static;">
    <div class="container-fluid">
        <ul class="nav navbar-nav">
            <li>
                <a href="http://www.12301.cc"> <i class="fa fa-reply-all"></i>
                    返回系统
                </a>
            </li>
        </ul>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <?php include('leftbar.php') ?>
        <div class="row col-xs-8">
            <div class="panel panel-primary ">
                <div class="panel-heading">
                    <h3 class="panel-title">b2c店铺手机端配置</h3>
                </div>
                <div class="panel-body">
                    <form>
                        <div class="form-group">
                            <label >窗口标题</label>
                            <input type="text" class="form-control" name="title" placeholder="请输入窗口标题" value="<?=$config['title']?>"></div>
                        <div class="form-group">
                            <label>幻灯片</label>
                            <textarea name="img_urls" id="myEditor" >
                                <? foreach($config['img_urls'] as $item):?>
                                   <p><a href="<?=$item['url']?>"><img src="<?=$item['img']?>"/></a></p>
                                <?endforeach;?>
                            </textarea>
                        </div>
                        <div class="form-group">
                            <label>类型显示</label>
                            <br/>
                            <label class="checkbox-inline">
                            <input type="checkbox" name="show_type" value="A" <?if (in_array('A', $config['types']))echo "checked";?>>景区
                            </label>
                            <label class="checkbox-inline">
                            <input type="checkbox" name="show_type" value="C" <?if (in_array('C', $config['types']))echo "checked";?>>酒店
                            </label>
                            <label class="checkbox-inline">
                            <input type="checkbox" name="show_type" value="B" <?if (in_array('B', $config['types']))echo "checked";?>>周边游
                            </label>
                            <label class="checkbox-inline">
                            <input type="checkbox" name="show_type" value="T" <?if (in_array('T', $config['types']))echo "checked";?>>套票
                            </label>
                            </div>
                        <div class="form-group theme" >
                            <label>推荐主题</label>
                            <a href="javascript:;" id="more_theme">更多</a>
                            <br/>
                            <?foreach($config['themes'] as $item):?>
                            <div>
                                <input type="checkbox" name="theme" checked>
                                类型
                                <select>
                                    <option value="A">景区</option>
                                    <option value="C" <?if ($item[0]=='C')echo "selected";?>>酒店</option>
                                    <option value="B" <?if ($item[0]=='B')echo "selected";?>>周边游</option>
                                    <option value="T" <?if ($item[0]=='T')echo "selected";?>>套票</option>
                                </select>
                                &nbsp;&nbsp;
                                主题名称
                                <select>
                                    <?foreach($themes as $theme):?>
                                    <option value="<?=$theme?>" <?if ($theme==$item[1])echo "selected";?>><?=$theme?></option>
                                    <?endforeach;?>
                                </select>
                                <a class="btn btn-danger btn-xs delete">删除</a>
                            </div>
                            <?endforeach;?>
                        </div>

                        <a class="btn btn-default save">保存</a>
                    </form>
                </div>
            </div>

        </div>
    </div>
</div>
<script>
    $(function() {
        //百度编辑器裁剪
        var ue = UE.getEditor('myEditor',
        {
            toolbars: [
                ['fullscreen', 'insertimage', 'link']
            ]
        });
        var themes = <?=json_encode($themes)?>;

        $("#more_theme").on('click', function() {
            if ($("input[name='theme']").length >= 4) {
                alert('最多只能添加四个主题');
                return false;
            }
            var tpl  = '<div>';
                tpl += '<input type="checkbox" name="theme" checked>';
                tpl += '&nbsp;类型&nbsp;';
                tpl += '<select>';
                tpl += '<option value="A">景区</option>';
                tpl += '<option value="C">酒店</option>';
                tpl += '<option value="B">周边游</option>';
                tpl += '<option value="T">套票</option>';
                tpl += '</select>';
                tpl += '&nbsp;&nbsp;&nbsp;&nbsp;主题名称&nbsp;';

                tpl += '<select>';
                for (var i = 0; i < themes.length; i++) {
                    tpl += '<option value="'+themes[i]+'">'+themes[i]+'</option>'
                }
                tpl += '</select>'
                tpl += '&nbsp;<a class="btn btn-danger btn-xs delete">删除</a>';
                tpl += '</div>';
            $(".theme").append(tpl);
        })

        $(".theme").delegate(".delete", "click", function() {
            $(this).parent().remove();
            return false;
        })

        $(".save").on("click", function() {            
            var themes = '';
            $(".theme>div").each(function(index) {
                if ($("input[name='theme']").eq(index).is(':checked')) {
                    themes += $(this).find('select').eq(0).val();
                    themes += '__';
                    themes += $(this).find('select').eq(1).val();
                    themes += '|';
                }
            })
            themes = themes.substring(0, themes.length - 1)

            var title = $("input[name='title']").val();
            var imgs = ue.getContent();
            var types = [];
            $("input[name='show_type']:checked").each(function() {
                types.push($(this).val())
            })
            types = types.toString()

            $.post(
                "b2c_store.php",
                {title : title,themes : themes,imgs : imgs,types : types},
                function(result) {
                    alert(result.info)
                },
                "json"
            )
        })
    })
</script>
</body>